package org.vectrics.recruiter.struts.admin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.vectrics.SystemException;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.user.Resume;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

public class JobCandidateEditAction extends VectricsStrutsAction {
	private transient static Logger log = Logger.getLogger(JobCandidateEditAction.class);
	
	public String doExecute(VectricsActionData actionData) throws JspException {
		Request request = actionData.getRequest();
		ActionForm form = actionData.getForm();
		
	    log.info("doExecute() called");
	    
		JobCandidateForm appForm = null;
		
		if (form == null) {
			appForm = new JobCandidateForm();
		} else {
			appForm = (JobCandidateForm)form;
		}
		
	    String forwardCode = "success";
		if (actionData.wasButtonPressed("cancel") == false) {
			
			String jobCandidateIdParam = request.getParameter("jobCandidateId");
			if (jobCandidateIdParam == null)
				jobCandidateIdParam = appForm.getJobCandidateId();
			if (jobCandidateIdParam == null)
				throw new SystemException("Could not find jobCandidateIdParam in request parameters or form");
			
			JobCandidate jobCandidate = RecruitServiceLocator.getJobProvider().findJobCandidateById(new Long(jobCandidateIdParam));
			
			appForm.setJobTitle(jobCandidate.getJob().getTitle());
			appForm.setCandidateName(jobCandidate.getCandidate().getPerson().getName());
			appForm.setJobCandidateId(jobCandidate.getId().toString());
			appForm.setJobId(jobCandidate.getJob().getId().toString());
			appForm.setPersonId(jobCandidate.getCandidate().getPerson().getId().toString());
			appForm.setStrength(jobCandidate.getStrength().toString());
			appForm.setNote(jobCandidate.getNote());
						
			if (jobCandidate.getResume() == null) {
				appForm.setResumeId("0");
			} else {
				appForm.setResumeId(jobCandidate.getResume().getId().toString());
			}
			String jobCandidateStatusCode = jobCandidate.getCurrentStatus().getStatusType().getCode();
			appForm.setStatusCode(jobCandidateStatusCode);
			actionData.setRequestForm(appForm);
	
			// Find list of possible status selections to go to.
			Collection statusTypes = RecruitServiceLocator.getJobProvider().findJobCandidateStatusTypesTo(jobCandidateStatusCode);
			if (statusTypes.size() == 0)
			    throw new SystemException("List of status to transition to is empty");
			request.setAttribute("jobCandidateStatusTypeList", statusTypes);
			
			List resumes = new ArrayList();
			Resume nullResume = new Resume();
			nullResume.setId(new Long(0));
			nullResume.setName("");
			resumes.add(nullResume);
			resumes.addAll(jobCandidate.getCandidate().getResumes());
			request.setAttribute("resumeList", resumes);
			
		} 
		
		return(forwardCode);
	}	
	
}
